package com.egd.imapp.contoller.activity.chat;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.egd.imapp.R;
import com.egd.imapp.contoller.activity.contact.ContactDetailActivity;
import com.egd.imapp.utils.Constant;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.ui.base.EaseBaseActivity;
import com.hyphenate.easeui.widget.EaseTitleBar;

public class SingleChatActivity extends AppCompatActivity {
    private EaseChatFragment easeChatFragment;
    private ImageView iv_single_chat;
    private EaseTitleBar mEaseTiTleBar;
    private EaseUser mEaseUser;
    private String conversationId;
    private int chatType;
    private String historyMsgId;
    private String TAG = "TAG";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        initIntent();

        initView();

        initData();

        initListener();
    }



    private void initIntent() {
        Intent intent = getIntent();
        mEaseUser = (EaseUser) intent.getSerializableExtra("user");

        conversationId = mEaseUser.getUsername();
        chatType = EaseConstant.CHATTYPE_SINGLE;
        historyMsgId = Constant.HISTORY_MSG_ID;
    }

    private void initView() {
        mEaseTiTleBar = findViewById(R.id.title_bar_message);;
        iv_single_chat = findViewById(R.id.iv_single_chat);;
//        setTitleBarRight();
    }

    private void initData() {
        mEaseTiTleBar.setTitle(mEaseUser.getUsername());

        // 创建会话的fragment
        easeChatFragment = new EaseChatFragment();


        Bundle bundle = new Bundle();
        bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, chatType);
//        bundle.putString(Constant.HISTORY_MSG_ID, historyMsgId);
//        bundle.putBoolean(EaseConstant.EXTRA_IS_ROAM, false);

        easeChatFragment.setArguments(bundle);

        Log.e(TAG, "initData: 替换聊天布局 mEaseUser = "+ mEaseUser  );
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_chat_fragment, easeChatFragment, "chat").commit();
//        getSupportFragmentManager().beginTransaction().add(R.id.container, easeChatFragment).commit();
    }

//    private void setTitleBarRight() {
//        if(chatType == Constant.CHATTYPE_SINGLE) {
//            mEaseTiTleBar.setRightImageResource(R.drawable.chat_user_info);
//        }else {
//            mEaseTiTleBar.setRightImageResource(R.drawable.chat_group_info);
//        }
//    }

    private void initListener() {

        mEaseTiTleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                onBackPressed();
            }
        });

        iv_single_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SingleChatActivity.this, ContactDetailActivity.class);
                intent.putExtra("user", mEaseUser);
                startActivity(intent);
            }
        });
    }
}